package org.example.service.imp;

import org.example.dto.UserPointUsageDTO;
import org.example.dto.PointRecordDTO;
import org.example.entity.PointRecords;
import org.example.entity.Users;
import org.example.mapper.PointRecordsMapper;
import org.example.mapper.UsersMapper;
import org.example.service.UserPointUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserPointUsageServiceImpl implements UserPointUsageService {
    
    @Autowired
    private UsersMapper usersMapper;
    
    @Autowired
    private PointRecordsMapper pointRecordsMapper;
    
    @Override
    public UserPointUsageDTO getUserPointUsageByOpenid(String openid) {
        try {
            // 根据openid查询用户信息
            Users user = usersMapper.getUserByOpenid(openid);
            if (user == null) {
                return null;
            }
            
            return getUserPointUsageByUserId(user.getId());
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    public UserPointUsageDTO getUserPointUsageByUserId(Long userId) {
        try {
            // 查询用户信息
            Users user = usersMapper.getUserById(userId);
            if (user == null) {
                return null;
            }
            
            // 查询积分记录
            List<PointRecords> pointRecords = pointRecordsMapper.getPointRecordsByUserId(userId);
            
            // 统计积分使用情况
            int totalEarned = 0;
            int totalSpent = 0;
            int totalBonus = 0;
            
            for (PointRecords record : pointRecords) {
                if (record.getType() == 1) { // 签到获得
                    totalEarned += record.getPoints();
                } else if (record.getType() == 2) { // 消费
                    totalSpent += record.getPoints();
                } else if (record.getType() == 3) { // 奖励
                    totalBonus += record.getPoints();
                }
            }
            
            int currentBalance = totalEarned + totalBonus - totalSpent;
            
            // 构建DTO
            UserPointUsageDTO dto = new UserPointUsageDTO();
            dto.setUserId(user.getId());
            dto.setOpenid(user.getOpenid());
            dto.setNickname(user.getNickname());
            dto.setAvatar(user.getAvatar());
            dto.setCurrentBalance(currentBalance);
            dto.setTotalEarned(totalEarned);
            dto.setTotalSpent(totalSpent);
            dto.setTotalBonus(totalBonus);
            dto.setTotalRecords(pointRecords.size());
            
            // 转换记录
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<PointRecordDTO> recordDTOs = pointRecords.stream()
                    .map(record -> {
                        PointRecordDTO dto1 = new PointRecordDTO();
                        dto1.setId(record.getId());
                        dto1.setType(record.getType());
                        dto1.setPoints(record.getPoints());
                        dto1.setDescription(record.getDescription());
                        dto1.setCreatedTime(sdf.format(record.getCreatedTime()));
                        
                        // 设置类型文本
                        switch (record.getType()) {
                            case 1:
                                dto1.setTypeText("签到获得");
                                break;
                            case 2:
                                dto1.setTypeText("消费");
                                break;
                            case 3:
                                dto1.setTypeText("奖励");
                                break;
                            default:
                                dto1.setTypeText("未知");
                        }
                        
                        return dto1;
                    })
                    .collect(Collectors.toList());
            dto.setRecords(recordDTOs);
            
            return dto;
        } catch (Exception e) {
            return null;
        }
    }
} 